<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="showAddPermissionDialog(1, '0')"
          >添加权限</el-button>
        </div>
        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="showAddPermissionDialog(2, row.id)"
              >添加权限点</el-button>
              <el-button size="small" type="text" @click="showEditPermissionDialog(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <addPermission ref="addPermission" :show-dialog.sync="showDialog" :type="type" :pid="pid" />
    </div>
  </div>
</template>

<script>
import { getPermissionListApi, delPermssionApi } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
import addPermission from './components/add-permission.vue'
export default {
  name: 'Permission',
  components: {
    addPermission
  },
  data() {
    return {
      permissionList: [],
      showDialog: false,
      type: 1,
      pid: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    showEditPermissionDialog(id) {
      this.showDialog = true
      this.$refs.addPermission.getPermissionDetail(id)
    },
    /**
     * 删除权限
     */
    delPermission(id) {
      this.$confirm('亲！您确认要删除吗', '温馨提示').then(async() => {
        await delPermssionApi(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      }).catch(() => {})
    },
    /**
     * 添加
     */
    showAddPermissionDialog(type, pid) {
      this.showDialog = true
      this.type = type
      this.pid = pid
    },
    /**
     * 获取权限列表
     */
    async getPermissionList() {
      const { data } = await getPermissionListApi()
      this.permissionList = transListToTreeData(data, '0')
    }
  }
}
</script>

<style>
</style>
